//
//  ShellWithdrawHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellWithdrawHeadView.h"
#import "ShellWithdrawViewModel.h"
#import "SWQRCodeViewController.h"
#import "ShellAccountTextField.h"
#import "ShellChangeTeadePasswordViewController.h"



@interface  ShellWithdrawHeadView()

@property (nonatomic,strong) ShellWithdrawViewModel *viewModel;

//可用数量
@property (nonatomic,strong) UILabel *availableNumLabel;

//矿工费
@property (nonatomic,strong) UILabel *feeNumLab;
//实际到账
@property (nonatomic,strong) UILabel *actualNumLabel;

//提示
@property (nonatomic,strong) UILabel *tipLabel;


//地址
@property (nonatomic,strong) ShellAccountTextField *addressTextField;
//数量
@property (nonatomic,strong) ShellAccountTextField *numberTextField;


@end
@implementation ShellWithdrawHeadView

- (instancetype)initViewModel:(ShellWithdrawViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellWithdrawViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = COLOR_TINT;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(K_SCREENWIDTH -30);
        make.height.mas_equalTo(213);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = k_imageName(@"icon_coin_bg");
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(contentView.mas_bottom);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH -30);
        make.height.mas_equalTo(137);
    }];
    
    ///!!!:提币地址
    UILabel *addressLab = [UILabel lz_labelWithText:LZLocalizedString(@"提币地址") fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
    [contentView addSubview:addressLab];
    [addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(27);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(200);
    }];
    
 
    
    [contentView addSubview:self.addressTextField];
    [self.addressTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(addressLab.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH - 60);
        make.height.mas_equalTo(40);
    }];
    
    
    ///!!!:提币数量
    UILabel *numberLab = [UILabel lz_labelWithText:LZLocalizedString(@"提币数量") fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
    [contentView addSubview:numberLab];
    [numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.addressTextField.mas_bottom).mas_offset(15);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(180);
    }];
    
    [contentView addSubview:self.availableNumLabel];
    [self.availableNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(numberLab.mas_centerY);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(150);
    }];
    
    [contentView addSubview:self.numberTextField];
    self.numberTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [self.numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(numberLab.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH - 60);
        make.height.mas_equalTo(40);
    }];
    
    
    ///!!!:矿工费用
    UILabel *feeLabel = [UILabel lz_labelWithText:LZLocalizedString(@"矿工费") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    [imageView addSubview:feeLabel];
    [feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(38);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(100);
    }];
    
    
    [imageView addSubview:self.feeNumLab];
    [self.feeNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(feeLabel.mas_centerY);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(160);
    }];
    
    
    ///!!!:实际到账
    UILabel *actualLabel = [UILabel lz_labelWithText:LZLocalizedString(@"实际到账") fontSize:k_textFont(14) color:UIColorHex(0x678DA8)];
    [imageView addSubview:actualLabel];
    [actualLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(feeLabel.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(130);
    }];
    
    
    [imageView addSubview:self.actualNumLabel];
    [self.actualNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(actualLabel.mas_centerY);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(160);
    }];
    
    self.feeNumLab.text = @"0";
    self.actualNumLabel.text = @"0";
    
    ///!!!:最少.最多
    [imageView addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(actualLabel.mas_bottom).mas_offset(12);
        make.bottom.mas_equalTo(-5);
        make.width.mas_equalTo(K_SCREENWIDTH - 60);
    }];

    ///!!!:申请提现
    UIButton *coinBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"申请提现") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    UIImage *highlightImage = [UIImage gradientImageWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] rect:CGRectMake(0, 0, 200, 50)];
    [coinBtn setBackgroundImage:highlightImage forState:UIControlStateNormal];
    [self addSubview:coinBtn];
    coinBtn.cornerRadius = 6;
    coinBtn.layer.shadowOffset = CGSizeMake(0,2);
    coinBtn.layer.shadowOpacity = 1;
    coinBtn.layer.shadowRadius = 6;
    [coinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(50);
    }];
    [coinBtn addTarget:self action:@selector(tradeCoinClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x intValue] == 4) {
            self.numberTextField.text = @"";
            self.actualNumLabel.text = @"0";
            NSString *tip = [NSString stringWithFormat:LZLocalizedString(@"单笔提币最少%@ %@，最多%@ %@"),
                             self.viewModel.withdrawDictionary[@"minWithdrawAmount"]?:@"",
                             self.viewModel.wallertModel.coinCode?:@"",
                             self.viewModel.withdrawDictionary[@"maxWithdrawAmount"]?:@"",
                             self.viewModel.wallertModel.coinCode?:@""];
            self.tipLabel.text = tip;
            
            NSString *fee = [NSString stringWithFormat:@"%@",self.viewModel.withdrawDictionary[@"fee"]];
            self.feeNumLab.text = [NSString stringWithFormat:@"%@ %@",fee,self.viewModel.wallertModel.coinCode];
            self.addressTextField.text = @"";
        }
    }];
    
    //计算矿工费, 实际到账
    [[self.numberTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        //计算手续费
        NSString *fee = [NSString stringWithFormat:@"%@",self.viewModel.withdrawDictionary[@"fee"]?:@"0"];
        NSString *actual = [self.numberTextField.text yw_stringBySubtracting:fee withRoundingMode:NSRoundDown scale:4];
        self.feeNumLab.text = [NSString stringWithFormat:@"%@ %@",fee,self.viewModel.wallertModel.coinCode?:@""];
        
        if([actual floatValue] < 0){
            self.actualNumLabel.text = @"0";
        } else {
            self.actualNumLabel.text = [NSString stringWithFormat:@"%@ %@",actual,self.viewModel.wallertModel.coinCode?:@""];
        }
    }];

    
    //可用
    NSString *useable = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.useable].saveDecimal;
    self.availableNumLabel.text = [NSString stringWithFormat:@"%@:%@",LZLocalizedString(@"可用"),useable];
}

//扫一扫
- (void)scanClick:(UIButton *)sender{
    // 校验相册权限
    @weakify(self);

    [LZ_BoolJudge lz_accessPrivacyPermissionWithType:LZ_PermissionTypeCamera completion:^(BOOL response) {
        if (response) {
            //使用模态呈现相册
            dispatch_async(dispatch_get_main_queue(), ^{
                
                SWQRCodeViewController *qeCodeVC = [[SWQRCodeViewController alloc] init];
                qeCodeVC.resultBlock = ^(NSString *result) {
                    @strongify(self);
                    self.addressTextField.text = result;
                };
                [LZTool.currentViewController.navigationController pushViewController:qeCodeVC animated:YES];

            });
        }
    }];
}


//全部
- (void)allNumClick:(UIButton *)sender{
    
    //可用
    NSString *useable = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.useable].saveDecimal;
    self.numberTextField.text = useable;
 
    CGFloat minWithdrawAmount = [self.viewModel.withdrawDictionary[@"minWithdrawAmount"] floatValue];
    if([useable floatValue] < minWithdrawAmount){
        NSString *tip = [NSString stringWithFormat:LZLocalizedString(@"单笔提币最少%@ %@，最多%@ %@"),
                         self.viewModel.withdrawDictionary[@"minWithdrawAmount"]?:@"",
                         self.viewModel.wallertModel.coinCode?:@"",
                         self.viewModel.withdrawDictionary[@"maxWithdrawAmount"]?:@"",
                         self.viewModel.wallertModel.coinCode?:@""];
        showDelayedDismissTitle(tip, nil);
        return;
    }
    
    
    
    NSString *fee = [NSString stringWithFormat:@"%@",self.viewModel.withdrawDictionary[@"fee"]];
    NSString *actual = [self.numberTextField.text yw_stringBySubtracting:fee].saveDecimal;
    self.feeNumLab.text = [NSString stringWithFormat:@"%@ %@",fee,self.viewModel.wallertModel.coinCode];
    self.actualNumLabel.text = [NSString stringWithFormat:@"%@ %@",actual,self.viewModel.wallertModel.coinCode];
    
    
}

//提现
- (void)tradeCoinClick:(UIButton *)sender{

    if(self.addressTextField.text.length == 0 || self.numberTextField.text.length == 0)return;
    @weakify(self);
    ShellUserDetailModel *detailModel = [LZToolCache getUserInfo];
    if(!detailModel.payPasswordFlag){
        //没有设置交易密码
        [LZToolView showAlertType:LZAlertViewTypeTipAlter withTitle:LZLocalizedString(@"提示") message:LZLocalizedString(@"请先设置交易密码") cancel:@"" sure:LZLocalizedString(@"确定") objectDict:@{@"textAlignment":@(NSTextAlignmentCenter)} cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {
            ShellChangeTeadePasswordViewController *changeTeadePasswordVC = [[ShellChangeTeadePasswordViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:changeTeadePasswordVC animated:YES];
              
        }];
        return;
    }
        
    [LZToolView showAlertType:LZAlertViewTypeTradePassword withTitle:LZLocalizedString(@"交易密码") message:LZLocalizedString(@"请输入交易密码") cancel:LZLocalizedString(@"取消") sure:LZLocalizedString(@"确定") objectDict:@{} cancelBlock:^(id  _Nullable object) { } sureBlock:^(id  _Nullable object) {
        @strongify(self);
        //发送请求 清算
        [self.viewModel tradeNumber:self.numberTextField.text address:self.addressTextField.text password:object];
    }];
    
    
    
}

#pragma mark - get && set
- (ShellAccountTextField *)addressTextField{
    if(!_addressTextField){
        _addressTextField = [[ShellAccountTextField alloc] init];
        _addressTextField.placeholder = LZLocalizedString(@"输入或者长按粘贴地址");
        _addressTextField.keyboardType = UIKeyboardTypeASCIICapable;

        UIView *rightView = [UIView new];
        rightView.frame = CGRectMake(0, 0, 60, 50);
        
        UIButton *scanBtn = [UIButton lz_buttonImageName:@"icon_scan" backgroundImageName:@""];
        scanBtn.frame = CGRectMake(20, 0, 40, 50);
        _addressTextField.rightView = rightView;
        [rightView addSubview:scanBtn];
        //设置显示模式为永远显示(默认不显示)
        _addressTextField.rightViewMode = UITextFieldViewModeAlways;
        [scanBtn addTarget:self action:@selector(scanClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressTextField;
}



- (ShellAccountTextField *)numberTextField{
    if(!_numberTextField){
        _numberTextField = [[ShellAccountTextField alloc] init];
        _numberTextField.placeholder = LZLocalizedString(@"请输入提币数量");
        _numberTextField.keyboardType = UIKeyboardTypeNumberPad;
        
        UIView *rightView = [UIView new];
        rightView.frame = CGRectMake(0, 0, 80, 50);
     
        UIButton *allBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"全部") titleColor:UIColorHex(0x8D99B2) fontSize:12];
        allBtn.frame = CGRectMake(30, 13, 50, 24);
        allBtn.bColor = UIColorHex(0x8D99B2);
        allBtn.bWidth = 1;
        allBtn.cornerRadius = 12;
        [allBtn addTarget:self action:@selector(allNumClick:) forControlEvents:UIControlEventTouchUpInside];
        [rightView addSubview:allBtn];

        _numberTextField.rightView = rightView;
        //设置显示模式为永远显示(默认不显示)
        _numberTextField.rightViewMode = UITextFieldViewModeAlways;
             
    }
    return _numberTextField;
}


- (UILabel *)availableNumLabel{
    if(!_availableNumLabel){
        _availableNumLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(18) color:UIColorHex(0x8D99B2)];
        [_availableNumLabel lz_rightAlignment];
    }
    return _availableNumLabel;
}


- (UILabel *)feeNumLab{
    if(!_feeNumLab){
        _feeNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
        [_feeNumLab lz_rightAlignment];
    }
    return _feeNumLab;
        
}


- (UILabel *)actualNumLabel{
    if(!_actualNumLabel){
        _actualNumLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
        [_actualNumLabel lz_rightAlignment];
    }
    return _actualNumLabel;
}

- (UILabel *)tipLabel{
    if(!_tipLabel){
        _tipLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0x4AB2E7)];
        _tipLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _tipLabel;
}


@end
